﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using UmcProvider.Shared;
using UmcProvider.PropertiesDesign;
using UmcControls.DesignInterface;
using System.Xml;
using System.ComponentModel.Design;
using System.Reflection;
using System.Text.RegularExpressions;
using Wicresoft.Shell.Core;

namespace Wicresoft.Shell.Package.AppExp.ToolboxControl
{
    //[Designer(typeof(UmcContainerDesigner<ITabControl>))]
    [DisplayName("TabControl")]
    [Description("Umc custom TabControl.")]
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(System.Windows.Forms.TabControl), "TabControl.bmp")]
    [UmcContorlGoupName("UMC Common Controls")]
    public class TabControl : System.Windows.Forms.TabControl, IUmcPropertyStore
    {
        //store the properties parse from code which is not surpported by designer
        //also store the properties which is set value using script variable
        //users should operator the dic through the IUmcPropertyStore
        private Dictionary<string, string> DesignViewUnSurpportProperty = new Dictionary<string, string>();

        #region IUmcPropertyStore Members

        public void AddUmcProperty(string property, string value)
        {
            DesignViewUnSurpportProperty.Add(property, value);
        }

        public void RemoveUmcProperty(string property)
        {
            DesignViewUnSurpportProperty.Remove(property);
        }

        public void RemoveAllUmcProperties()
        {
            DesignViewUnSurpportProperty.Clear();
        }

        public string GetUmcProperty(string property)
        {
            if (DesignViewUnSurpportProperty.ContainsKey(property))
            {
                return DesignViewUnSurpportProperty[property];
            }
            else
            {
                return null;
            }
        }

        public List<KeyValuePair<string, string>> GetUmcPropertyList()
        {
            return DesignViewUnSurpportProperty.ToList();
        }


        #endregion

    }


    public sealed class UmcTabControl : DefaultConverter<UmcTabControl, TabControl>, ITabControl
    {
        #region ITabControl Members

        public string Name
        {
            get { throw new NotImplementedException(); }
        }

        public bool Enabled
        {
            get { throw new NotImplementedException(); }
        }

        public Size Size
        {
            get { throw new NotImplementedException(); }
        }

        public Point Location
        {
            get { throw new NotImplementedException(); }
        }

        public string Text
        {
            get { throw new NotImplementedException(); }
        }

        public Color BackColor
        {
            get { throw new NotImplementedException(); }
        }

        public Font Font
        {
            get { throw new NotImplementedException(); }
        }

        public Color ForeColor
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        public override XmlNode[] Serialize(System.ComponentModel.IComponent root, XmlDocument document)
        {
            XmlNode[] nodes = base.Serialize(root, document);

            if (nodes.Length > 0)
            {
                XmlNode tabControlNode = nodes[0];
                TabControl tabControl = root as TabControl;
                if (tabControl != null)
                {
                    TabControl.TabPageCollection tabPages = tabControl.TabPages;
                    foreach (TabPage tabPage in tabPages)
                    {
                        XmlNode tabPageNode = document.CreateNode(XmlNodeType.Element, "TabPage", null);
                        tabPageNode.Attributes.Append(document.CreateAttribute("Text"))
                            .Value = tabPage.Text;
                        tabPageNode.Attributes.Append(document.CreateAttribute("BackColor"))
                            .Value = UmcPropertyConvert.ToString(tabPage.BackColor);
                        tabControlNode.AppendChild(tabPageNode);

                        //                      
                        foreach (IComponent child in tabPage.Controls)
                        {
                            IConverter ict = UmcSerializeFactory.Create(child);
                            if (ict != null)
                            {
                                XmlNode[] xn = ict.Serialize(child, document);
                                tabPageNode.AppendChild(xn.First());
                            }
                        }
                    }
                }
            }

            return nodes;
        }

        public override IComponent DeSerialize(IDesignerHost host, XmlDocument document, XmlNode[] node)
        {
            IComponent component = base.DeSerialize(host, document, node);
            TabControl tabControl = component as TabControl;

            if (node.Length > 0 && tabControl != null)
            {
                XmlNode tabControlNode = node[0];
                foreach (XmlNode tabPageNode in tabControlNode.ChildNodes)
                {
                    if (tabPageNode.NodeType == XmlNodeType.Element && tabPageNode.Name == "TabPage")
                    {
                        TabPage tabPage = new TabPage();
                        if (!string.IsNullOrEmpty(tabPageNode.Attributes["Text"].Value))
                        {
                            PropertyInfo prop = typeof(TabPage).GetProperty("Text");
                            prop.SetValue(tabPage, AttributeReplace(tabPageNode.Attributes["Text"].Value), null);
                        }
                        try
                        {
                            string attr = AttributeReplace(tabPageNode.Attributes["BackColor"].Value);
                            tabPage.BackColor = UmcPropertyConvert.ToColor(attr);
                        }
                        catch
                        {
                            tabPage.BackColor = Color.FromKnownColor(KnownColor.Control);
                        }
                        tabControl.TabPages.Add(tabPage);

                        foreach (XmlNode controlInTabPageNode in tabPageNode.ChildNodes)
                        {
                            IConverter ict = UmcDeSerializeFactory.Create(controlInTabPageNode.Name);
                            IComponent componentInTabPage = ict.DeSerialize(host, document, new XmlNode[] { controlInTabPageNode });
                            Control controlInTabPage = componentInTabPage as Control;
                            if (controlInTabPage != null)
                            {
                                tabPage.Controls.Add(controlInTabPage);
                            }
                        }
                    }
                }
            }

            return component;
        }
    }


    public interface ITabControl : IUmc
    {
        string Name { get; }
        bool Enabled { get; }
        Size Size { get; }
        Point Location { get; }
        string Text { get; }
        Color BackColor { get; }
        Font Font { get; }
        Color ForeColor { get; }
    }

}
